package com.hmall.trade.config;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign调用拦截器
 *
 * @Author mr.wu
 * @Date 2025-8-23 15:54
 */
@Configuration
public class FeignInterceptor {


    @Bean
    public RequestInterceptor requestInterceptor () {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userId = UserContext.getUser();//从当前微服务的ThreadLocal中获取用户ID

                template.header("user-info",userId.toString());//设置用户ID请求头，再调用服务提供者的接口
            }
        };
    }
}
